import jwt

from core.ext import sio
from core.auth import decode_token


@sio.event
async def connect(sid, environ, auth):
    try:
        # print("auth", auth)
        token = auth['token']
        user = decode_token(token)
        await sio.save_session(sid, user)
    except jwt.ExpiredSignatureError:
        await sio.emit('status', 409, to=sid)
    except Exception as e:
        print(e)
        await sio.disconnect(sid)


@sio.event
async def disconnect(sid):
    print('disconnect ', sid)


@sio.event
async def message(sid, data):
    session = await sio.get_session(sid)
    print(session)
    print(data)
